#include <iostream>
#include <cstdio>
#include <set>
#include <cstring>

#define eb emplace_back
#define ep emplace
#define fi first
#define se second
#define in read<int>()
#define lin read<ll>()
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

#define ll long long

template < typename T > T read() {
	T x = 0; bool f = 0; char ch = getchar();
	while(!isdigit(ch)) f |= ch == '-', ch = getchar();
	while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}

template < typename T > void chkmax(T &x, const T &y) { x = x > y ? x : y; }
template < typename T > void chkmin(T &x, const T &y) { x = x < y ? x : y; }

const int N = 1e6 + 10;

int n, a[N];
ll M, s[N], dp[N];
int q[N], h, t;

int main() {
#ifdef YJR_2333_TEST
	freopen("1.in", "r", stdin);
#endif
	n = in, M = lin; rep(i, 1, n) a[i] = in, s[i] = s[i - 1] + a[i];
	h = 1, t = 1; q[1] = 0; a[0] = 1e9; int pos = 0;
	rep(i, 1, n) if(a[i] > M) return puts("-1"), 0;
	rep(i, 1, n) {
		while(s[i] - s[pos] > M) pos++;
		while(h <= t && s[i] - s[q[h]] > M) h++;
		while(h <= t && a[i] > a[q[t]]) t--;
		q[++t] = i; dp[i] = dp[pos] + a[q[h]];
		rep(j, h, t - 1) chkmin(dp[i], dp[q[j]] + a[q[j + 1]]);
	} cout << dp[n] << endl;
	return 0;
}
